import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**

 * hidden: true                   展示菜单
 * alwaysShow: true               必展示菜单
 *                                如果false, 当多于一个子路由,
 *                                就会变成nested mode, 否则不展示跟菜单
 * redirect: noRedirect           noRedirect则不会出现在面包屑
 * name:'router-name'             必须设置 <keep-alive> 会用到缓存页面
 * meta : {
    roles: ['admin','editor']    角色设置，本项目剔除这个，采用后端权限控制
    title: 'title'               这个用于左侧菜单和面包屑命名
    icon: 'svg-name'/'el-icon-x' 左侧菜单的icon
    noCache: true                true则不缓存(default is false)
    affix: true                  if set true, 会加入标签栏管控
    breadcrumb: false            if set false不会出现在面包屑
    activeMenu: '/example/list'  重设参数，让对应的左侧标签栏高亮
  }
 */

export const constantRoutes = [
  {
    path: "/404",
    component: () => import("@/views/error-page/404"),
    hidden: true
  },
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true
  },
  {
    path: "/",
    component: Layout, // 左侧的每个菜单都使用layout是因为，多个层级时，能够根据第一层路由分类
    redirect: "/home",
    children: [
      {
        path: "home",
        component: () => import("@/views/home/index"),
        name: "Home",
        meta: { title: "首页", icon: "el-icon-s-shop", affix: true }
      }
    ]
  },
  {
    path: "/test",
    component: Layout,
    redirect: "/test/index",
    children: [
      {
        path: "index",
        component: () => import("@/views/test/index"),
        name: "Test",
        meta: { title: "测试", icon: "icon-zhanghu", noCache: true }
      }
    ]
  }
];

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = [
  // 404 page must be placed at the end !!!
  { path: "*", redirect: "/404", hidden: true }
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  });

const router = createRouter();

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}

export default router;
